package cn.shengchao.cap.acl.adapters.repositories;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.shengchao.cap.acl.ports.repositories.IProductThirdCapOrderGradeRepository;
import cn.shengchao.cap.domain.order.CapOrderCreateDto;
import cn.shengchao.cap.domain.order.CapOrderUtils;
import cn.shengchao.cap.infrastructure.dao.ProductThirdCapOrderGradeDao;
import cn.shengchao.cap.infrastructure.dao.po.ProductThirdCapOrderGrade;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @program: examstarapi
 * @description: 高考志愿服务订单的用户年级记录
 * @author: Xiong weiqiang
 * @create: 2025-05-21 19:30
 */

@Repository
public class ProductThirdCapOrderGradeRepository implements IProductThirdCapOrderGradeRepository {
    @Autowired
    private ProductThirdCapOrderGradeDao productThirdCapOrderGradeDao;
    @Autowired
    private CapOrderUtils capOrderUtils;

    @Transactional
    @Override
    public void saveObj(String orderId, long userId, Integer grade) {
        ProductThirdCapOrderGrade productThirdCapOrderGrade = ProductThirdCapOrderGrade.builder()
                .orderId(orderId)
                .userId(userId)
                .graduationYear(grade)
                .deleted(0)
                .build();
        // 先删除重复的记录
        productThirdCapOrderGradeDao.delete(Wrappers.lambdaQuery(ProductThirdCapOrderGrade.class)
                .eq(ProductThirdCapOrderGrade::getOrderId, orderId)
                .eq(ProductThirdCapOrderGrade::getUserId, userId)
        );
        productThirdCapOrderGradeDao.insert(productThirdCapOrderGrade);
    }

    @Override
    public Integer syncToThird(String orderId, long userId, String phone){
        ProductThirdCapOrderGrade productThirdCapOrderGrade = productThirdCapOrderGradeDao.selectOne(Wrappers.lambdaQuery(ProductThirdCapOrderGrade.class)
                .eq(ProductThirdCapOrderGrade::getOrderId, orderId)
                .eq(ProductThirdCapOrderGrade::getUserId, userId)
        );
        if (productThirdCapOrderGrade != null) {
            String thirdOrderCode = capOrderUtils.createOrder(CapOrderCreateDto.builder()
                    .orderCode(productThirdCapOrderGrade.getOrderId())
                    .graduationYear(productThirdCapOrderGrade.getGraduationYear())
                    .userName(phone)
                    .build());
            if (StrUtil.isNotBlank(thirdOrderCode)) {
                productThirdCapOrderGradeDao.update(productThirdCapOrderGrade, Wrappers.lambdaUpdate(ProductThirdCapOrderGrade.class)
                        .eq(ProductThirdCapOrderGrade::getOrderId, orderId)
                        .eq(ProductThirdCapOrderGrade::getUserId, userId)
                        .set(ProductThirdCapOrderGrade::getOrderCode, thirdOrderCode)
                        .set(ProductThirdCapOrderGrade::getUserName, phone)
                );
            } else {
                productThirdCapOrderGradeDao.update(productThirdCapOrderGrade, Wrappers.lambdaUpdate(ProductThirdCapOrderGrade.class)
                        .eq(ProductThirdCapOrderGrade::getOrderId, orderId)
                        .eq(ProductThirdCapOrderGrade::getUserId, userId)
                        .set(ProductThirdCapOrderGrade::getOrderCode, -1L)
                        .set(ProductThirdCapOrderGrade::getUserName, phone)
                );
            }
        }
        return productThirdCapOrderGrade != null ? productThirdCapOrderGrade.getGraduationYear() : -1;
    }

    @Override
    public boolean isOrderValidForUser(Long userId) {
        int dateNum = productThirdCapOrderGradeDao.queryMaxOrderValidTimeForUserId(userId);
        if (dateNum != 0) {
            Date date = DateUtil.parse(dateNum + "-09-01");
            return date != null && DateUtil.compare(date, new Date()) >= 1;
        }
        return false;
    }
}